package com.ejuornal.service.impl;

import java.sql.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ejuornal.bean.Ads;
import com.ejuornal.bean.Page;
import com.ejuornal.bean.vo.AdsVO;
import com.ejuornal.dao.AdsDao;
import com.ejuornal.service.AdsService;

@Component("adsService")
public class AdsServiceImpl implements AdsService {
	public AdsDao adsDao;

	public void add(Ads ads) {
		String sql = "insert into t_ads values(?,?,?,?,?,?,?,?)";
		adsDao.add(sql, new Object[] { 
									null, 
									ads.getPicSrc(), 
									ads.getAdsPosId(),
									ads.getBeginDate(), 
									ads.getEndDate(), 
									ads.getAdsName(),
									ads.getRemarks(),
									ads.getUrl()});
	}

	public void update(Ads ads) {
		String sql = "update t_ads set picSrc = ?,beginDate=?,endDate= ?, adsName = ?,remarks= ?,url = ? where adsId=?";
		adsDao.update(
				sql,
				new Object[] { ads.getPicSrc(), ads.getBeginDate(),
						ads.getEndDate(), ads.getAdsName(), ads.getRemarks(),ads.getUrl(),
						ads.getAdsId() });
	}

	public void delete(int[] adsIds) {
		String sql = "delete from t_ads where adsId = ?";
		adsDao.batchDelete(sql, adsIds);

	}

	public Ads findByAdsId(int adsId) {
		Ads ads = adsDao.findById(adsId);
		return ads;
	}
	
	@Override
	public Ads findAdsByPosIdAndTime(int posId) {
		Ads ads = adsDao.getAdsByPosIdAndTime(posId);
		return ads;
	}


	public Page<Ads> findByAdsPosIdEndDate(int current_record, String url,
			int adsPosId, Date day) {
		String hsql = "WHERE adsPosId = ? AND endDate > ? ORDER BY endDate ASC";

		return adsDao.find(current_record, url, hsql, new Object[]{ adsPosId, day });
	}

	public Page<Ads> findByAdsPosIdBeginDate(int current_record, String url,
			int adsPosId, Date day) {
		String hsql = "WHERE adsPosId = ? AND beginDate > ? ORDER BY endDate ASC";

		return adsDao.find(current_record, url, hsql, new Object[]{ adsPosId, day });
	}

	public Page<Ads> findByAdsPosId(int current_record, String url, int adsPosId) {
		String hsql = "WHERE adsPosId = ?";

		return adsDao.find(current_record, url, hsql, new Object[]{ adsPosId });
	}

	public Page<Ads> findAll(int current_record, String url) {
		return adsDao.find(current_record, url);
	}
	
	@Override
	public AdsVO findByAdsVOId(int adsId) {
		return adsDao.findByIdVo(adsId);
	}

	public AdsDao getAdsDaoImpl() {
		return adsDao;
	}

	@Resource(name = "adsDao")
	public void setAdsDaoImpl(AdsDao adsDaoImpl) {
		this.adsDao = adsDaoImpl;
	}

    @Override
    public boolean updateLoginImg(String url) {
        return adsDao.updateLoginImg(url);
    }

	
}
